#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import pygtk
pygtk.require('2.0')

import gtk
import gnomeapplet

import sys
import logging

from timelogic import Task
from taskmanager import TaskManager

class TimeKeeperApplet(gnomeapplet.Applet):

    APP_ICON_CLOCK = 'images/time-keeper-clock-24x24.png'
    APP_ICON_PAUSE = 'images/time-keeper-pause-24x24.png'
    TASKS_DATA_XML = 'data/tasks.xml'

    APP_STATE_RUN = "run"
    APP_STATE_IDLE = "idle"

    def __init__(self,applet,iid):
        self.applet = applet
        self.init_core_widgets()
        self.init_other_widgets()
        self.init_menu()
        self.connect_events()
        self.init_state()
        self.applet.show_all()

        self.manager = TaskManager(TimeKeeperApplet.TASKS_DATA_XML)

    def init_state(self):
        self.mode = TimeKeeperApplet.APP_STATE_IDLE

    def init_core_widgets(self):
        # Create boxes
        self.hbox = gtk.HBox()
        self.eventbox = gtk.EventBox()
        # Add the boxes to the applet
        self.hbox.add(self.eventbox)
        self.applet.add(self.hbox)

    def init_other_widgets(self):
        # Create tooltips and image
        self.tooltips = gtk.Tooltips()
        self.image = gtk.Image()
        self.tooltips.set_tip(self.image, "")
        
        size = self.applet.get_size()
        self.clock_pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(TimeKeeperApplet.APP_ICON_CLOCK, size, size)
        self.pause_pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(TimeKeeperApplet.APP_ICON_PAUSE, size, size)
        self.image.set_from_pixbuf(self.clock_pixbuf)

        # Add to the eventbox
        self.eventbox.add(self.image)

    def init_menu(self):
        menu_xml = """
        <popup name="button3">
            <menuitem name="About Item" verb="About" stockid="gtk-about" />
            <menuitem name="Preferences Item" verb="Preferences" stockid="gtk-preferences" />
        </popup>
        """
        menu_verbs = [("About", self.cb_about), ("Preferences", self.cb_preferences)]
        self.applet.setup_menu(menu_xml, menu_verbs, None)

    def connect_events(self):
        self.eventbox.connect("button-press-event", self.event_on_button)
        self.eventbox.connect("enter-notify-event", self.event_on_enter)
        self.eventbox.connect("destroy", self.event_cleanup)
        self.button_actions = {1: self.cb_switch_mode, 2: lambda: None, 3: lambda: None}

    def event_cleanup(self, widget):
        del self.applet

    def event_on_button(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            self.button_actions[event.button]()

    def event_on_enter(self, widget, event):
        if self.mode == TimeKeeperApplet.APP_STATE_IDLE:
            state = "(Stopped)"
        else:
            state = "(Running)"
        self.tooltips.set_tip(self.image, "%s %s" %
                              (self.manager.taskInfo(), state))

    def cb_switch_mode(self):
        if self.mode == TimeKeeperApplet.APP_STATE_IDLE:
            self.mode = TimeKeeperApplet.APP_STATE_RUN
            self.image.set_from_pixbuf(self.pause_pixbuf)
            self.manager.run()
        elif self.mode == TimeKeeperApplet.APP_STATE_RUN:
            self.mode = TimeKeeperApplet.APP_STATE_IDLE
            self.image.set_from_pixbuf(self.clock_pixbuf)
            self.manager.halt()

    def cb_preferences(self, event, data=None):
        print("blalala");

    def cb_about(self, event, data=None):
        adlg = gtk.AboutDialog()

        adlg.set_name('Time Keeper Applet')
        adlg.set_version('0.01')
        adlg.set_comments('Displays and tracks time.')
        adlg.set_website('http://code.google.com/p/time-keeper-applet/')
        adlg.set_website_label('http://google.com')
        adlg.set_authors(('dmitrim',))

        adlg.run()
        adlg.destroy()



def applet_factory(applet, iid): 
    TimeKeeperApplet(applet, iid)
    return True

def main(argv):
    gnomeapplet.bonobo_factory('OAFIID:GNOME_TimeKeeperApplet_Factory',
        TimeKeeperApplet.__gtype__,
        "Time Keeper Applet",
        "0",
        applet_factory)

if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == '-d': 
        mainWindow = gtk.Window()
        mainWindow.set_title('Applet window')
        mainWindow.connect('destroy', gtk.main_quit)
        applet = gnomeapplet.Applet()
        applet_factory(applet, None)
        applet.reparent(mainWindow)
        mainWindow.show_all()
        gtk.main()
        sys.exit()
    else: 
        main(sys.argv)

